package com.gragra.functionalIdentification.tests;
import static org.junit.Assert.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import org.junit.Before;
import org.junit.Test;
import com.gragra.data.Corpus;
import com.gragra.functionalIdentification.FunctionalInsteadOfNoun;
import com.gragra.sampling.sequentialStructure.ExtensionLevels;
import com.gragra.sampling.sequentialStructure.WordAndTagHolder;
import com.gragra.sampling.sequentialStructure.WordType;
import com.gragra.sequentiaDataGenerator.BaseGenerator;
import com.gragra.sequentiaDataGenerator.CaseLevelAssigningSegmenter;
import com.gragra.sequentiaDataGenerator.PunctuationSplitOffSegmenter;
public class TestFunctionalInsteadOfNoun
{
	/**
	 * 
	 */
	private Corpus<WordAndTagHolder>  corp;
	/**
	 * 
	 */
	private FunctionalInsteadOfNoun fin;
	@Before
	public void setUp() throws Exception
	{
		StringReader sr = new StringReader("A|NONE|WordTypeLevel|Function Cat|1|WordTypeLevel|Noun is|0 on|NONE|WordTypeLevel|Function the|0 mat.|NONE\nA|g horse|NONE is|0 on|NONE the|0 grass.|NONE\nA|j mouse|NONE is|0 in|NONE the|0 trap.|NONE");
		BufferedReader br = new BufferedReader(sr);
		BaseGenerator bg = new BaseGenerator(new CaseLevelAssigningSegmenter(new PunctuationSplitOffSegmenter("NONE")));
		corp = bg.createCorpus(br);
		fin = new FunctionalInsteadOfNoun(corp, 3, 1, 1, 4, 8, 20);
		fin.identify();
	}
	@Test
	public void testAnnotateCorpusFrames()
	{
		fin.annotateCorpusFrames(corp);
		WordAndTagHolder wth = corp.getEntry(1);
		assertEquals(wth.getExtensionInformation().getFiniteInformation(0, ExtensionLevels.WordTypeLevel),null);
		assertEquals(wth.getExtensionInformation().getFiniteInformation(1, ExtensionLevels.WordTypeLevel),null);
		assertEquals(wth.getExtensionInformation().getFiniteInformation(2, ExtensionLevels.WordTypeLevel),WordType.Function);
		assertEquals(wth.getExtensionInformation().getFiniteInformation(3, ExtensionLevels.WordTypeLevel),null);
		assertEquals(wth.getExtensionInformation().getFiniteInformation(4, ExtensionLevels.WordTypeLevel),WordType.Function);
		assertEquals(wth.getExtensionInformation().getFiniteInformation(3, ExtensionLevels.WordTypeLevel),null);
		assertEquals(wth.getExtensionInformation().getFiniteInformation(4, ExtensionLevels.WordTypeLevel),WordType.Function);
	}
	@Test
	public void testEvaluate()
	{
		double[] arr = fin.evaluate(corp, true);
		assertEquals(arr[1],0.5,0.0000001);
		assertEquals(arr[0],0.08333333333333,0.000000001);
	}
	@Test
	public void testWriteOutFrames() throws IOException
	{
		StringWriter sw = new StringWriter();
		BufferedWriter bw = new BufferedWriter(sw);
		fin.writeOutFrames(bw);
		bw.close();
		String[] frames = sw.toString().split("\n");
		assertEquals("< on _ grass >",frames[0].trim());
		assertEquals("< mouse _ in >",frames[1].trim());
		assertEquals("< in _ trap >",frames[2].trim());
		assertEquals("< trap _ BORDERMARKER >", frames[3].trim());
		assertEquals("< grass _ BORDERMARKER >", frames[4].trim());
		assertEquals("< horse _ on >", frames[5].trim());
		assertEquals("< mat _ BORDERMARKER >", frames[6].trim());
		assertEquals("< BORDERMARKER _ mouse >", frames[7].trim());
		assertEquals(8,frames.length);
	}
	@Test
	public void testWriteOutWords() throws IOException
	{
		StringWriter sw = new StringWriter();
		BufferedWriter bw = new BufferedWriter(sw);
		fin.writeOutWords(bw);
		bw.close();
		String[] words = sw.toString().split("\n");
		assertEquals(".",words[0].trim());
		assertEquals("the",words[1].trim());
		assertEquals("is",words[2].trim());
		assertEquals("a",words[3].trim());
	}
	@Test
	public void testAnnotateCorpusWords()
	{
		fin.annotateCorpusWords(corp);
		WordAndTagHolder wth = corp.getEntry(1);
		assertEquals(wth.getExtensionInformation().getFiniteInformation(0, ExtensionLevels.WordTypeLevel),WordType.Function);
		assertEquals(wth.getExtensionInformation().getFiniteInformation(1, ExtensionLevels.WordTypeLevel),null);
		assertEquals(wth.getExtensionInformation().getFiniteInformation(2, ExtensionLevels.WordTypeLevel),WordType.Function);
		assertEquals(wth.getExtensionInformation().getFiniteInformation(3, ExtensionLevels.WordTypeLevel),null);
		assertEquals(wth.getExtensionInformation().getFiniteInformation(4, ExtensionLevels.WordTypeLevel),WordType.Function);
		assertEquals(wth.getExtensionInformation().getFiniteInformation(3, ExtensionLevels.WordTypeLevel),null);
		assertEquals(wth.getExtensionInformation().getFiniteInformation(4, ExtensionLevels.WordTypeLevel),WordType.Function);		
	}
}